//
//  YMBaseViewController.m
//  GuoAo
//
//  Created by y s on 2023/3/9.
//

#import "YMBaseViewController.h"

#import "UINavigationController+FDFullscreenPopGesture.h"

@interface YMBaseViewController ()

@property (nonatomic,copy) NSString *showTitle;

@end

@implementation YMBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = STheme.D14;
    self.hideNavLine  = NO;
    self.isWhiteStatusBar = NO;
    _barTintColor = STheme.D9;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(![SJumpUtil isTop:self]){
        return;
    }
    [self resetBars];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}

// 重置
- (void)resetBars{
    if(![SJumpUtil isTop:self]){
        return;
    }
    self.fd_prefersNavigationBarHidden = _navBarHidden;
    [self.navigationController setNavigationBarHidden:_navBarHidden animated:true];
    
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *navBar = [[UINavigationBarAppearance alloc] init];
        navBar.backgroundColor = _barTintColor;
        self.navigationController.navigationBar.scrollEdgeAppearance = navBar;
        self.navigationController.navigationBar.standardAppearance = navBar;
    } else {
        self.navigationController.navigationBar.barTintColor = _barTintColor;
    }
    self.navigationController.navigationBar.translucent=false;
    [self setNeedsStatusBarAppearanceUpdate];
    [self setStatusBarAlpha:_statusBarAlpha];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    self.navigationController.navigationBar.barStyle = (_isWhiteStatusBar ? UIBarStyleBlack:UIBarStyleDefault);
    UIStatusBarStyle style = (_isWhiteStatusBar ? UIStatusBarStyleLightContent:UIStatusBarStyleDefault);
    return style;
}

// 设置导航栏是否隐藏
- (void)setNavBarHidden:(BOOL)navBarHidden{
    if(![SJumpUtil isTop:self]){
        return;
    }
    _navBarHidden = navBarHidden;
    self.fd_prefersNavigationBarHidden = _navBarHidden;
    [self.navigationController setNavigationBarHidden:_navBarHidden animated:true];
}
// 设置状态色是否为白色
- (void)setIsWhiteStatusBar:(BOOL)isWhiteStatusBar{
    _isWhiteStatusBar = isWhiteStatusBar;
    if(![SJumpUtil isTop:self]){
        return;
    }
    [self setNeedsStatusBarAppearanceUpdate];
    self.navigationController.navigationBar.barStyle=(_isWhiteStatusBar?UIBarStyleBlack:UIBarStyleDefault);
}
// 设置状态条是否隐藏
- (void)setStatusBarHidden:(BOOL)statusBarHidden{
    _statusBarHidden=statusBarHidden;
    if(![SJumpUtil isTop:self]){
        return;
    }
    [self setNeedsStatusBarAppearanceUpdate];
}
- (BOOL)prefersStatusBarHidden
{
    return _statusBarHidden;
}

// 设置状态条是否隐藏
- (void)setStatusBarAlpha:(BOOL)statusBarAlpha{
    _statusBarAlpha = statusBarAlpha;
    if(![SJumpUtil isTop:self]){
        return;
    }
    if (@available(iOS 13.0, *)) {
        
    }else{
        UIView *statusBar=[[UIApplication sharedApplication] valueForKey:@"statusBar"];
        if(statusBar){
            statusBar.alpha=_statusBarAlpha ?0:1;
        }
    }
}

- (void)showNavTitle:(NSString *)title{
    if(title.length<1 || [_showTitle isEqualToString:title]){
        return;
    }
    if(![SJumpUtil isTop:self]){
        return;
    }
    [SNavUtil resetTitle:self title:title];
    [self resetBars];
}



- (void)setHideNavLine:(BOOL)hideNavLine{
    UIImageView *navBarBottomLine = [self findBottomLineInView:self.navigationController.navigationBar];
    if (hideNavLine) {
        navBarBottomLine.hidden = YES;
    }else{
        navBarBottomLine.image = [SUIUtil imageOfColor:[SUIUtil colorOf:0xeeeeee]];
    }
}

- (UIImageView *)findBottomLineInView:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
         UIImageView *imageView = [self findBottomLineInView:subview];
         if (imageView) {
             return imageView;
       }
    }
   return nil;
}


@end
